#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::unique_ptr;
using std::shared_ptr;

class Point
: public std::enable_shared_from_this<Point>
{
public:
    Point(int ix = 0,int iy = 0)
        : _ix(ix)
          , _iy(iy)
    {
        cout << "Point(int=0,int=0)" << endl;
    }

    void print() const{
        cout << "(" << _ix
            << "," << _iy
            << ")" << endl;
    }

    shared_ptr<Point> addPoint(Point * pt){
        _ix += pt->_ix;
        _iy += pt->_iy;
        return shared_from_this();
        /* return shared_ptr<Point>(this); */
        /* return this; */
    }

    ~Point(){
        cout << "~Point()" << endl; 
    }
private:
    int _ix;
    int _iy;
};

void test0(){
    shared_ptr<Point> sp(new Point(1,2));
    shared_ptr<Point> sp2(new Point(3,4));

    //创建sp3的参数实际上是sp所对应的裸指针
    //效果还是多个智能指针托管了同一块空间
    shared_ptr<Point> sp3(sp->addPoint(sp2.get()));
    cout << "sp3 = ";
    sp3->print();
}

int main(void){
    test0();
    return 0;
}
